package com.autoapitest.system.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.autoapitest.common.core.domain.entity.ProjectBaseEntity;
import com.autoapitest.common.enums.DeleteStatus;
import com.autoapitest.common.exception.ServiceException;
import com.autoapitest.common.utils.SecurityUtils;
import com.autoapitest.common.utils.bean.BeanUtils;
import com.autoapitest.system.domain.ProjectBase;
import com.autoapitest.system.mapper.ProjectBaseMapper;
import com.autoapitest.system.service.IProjectBaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;

/**
 * 项目管理 服务层实现
 *
 * @author yangsen
 */
@Service
public class ProjectBaseServiceImpl extends ServiceImpl<ProjectBaseMapper, ProjectBase> implements IProjectBaseService {



    /**
     * 项目管理新增
     *
     * @param projectBaseEntity 项目管理
     * @return 结果
     */

    @Override
    public int insertProject(ProjectBaseEntity projectBaseEntity) {
        ProjectBase projectBase=new ProjectBase();
        BeanUtils.copyBeanProp(projectBase,projectBaseEntity);
        projectBase.setDelFlag(DeleteStatus.OK.getCode());
        projectBase.setCreateBy(SecurityUtils.getUserId().toString());
        projectBase.setCreateTime(new Date());
        return baseMapper.insert(projectBase);
    }


    /**
     * 项目管理修改
     *
     * @param projectBaseEntity 项目管理
     * @return 结果
     */
    @Override
    public int updateProject(ProjectBaseEntity projectBaseEntity) {
        ProjectBase projectBase=new ProjectBase();
        BeanUtils.copyBeanProp(projectBase,projectBaseEntity);
        projectBase.setUpdateBy(SecurityUtils.getUserId().toString());
        projectBase.setUpdateTime(new Date());
        return baseMapper.updateById(projectBase);
    }

    /**
     * 项目管理新增 修改校验
     *
     * @param projectBaseEntity 项目管理
     * @return 结果
     */
    @Override
    public Boolean checkProject(ProjectBaseEntity projectBaseEntity, Boolean bool) {
        if (bool){
            //校验名称是否重复
            LambdaQueryWrapper<ProjectBase>lambdaQueryWrapper=new LambdaQueryWrapper<>();
            LambdaQueryWrapper<ProjectBase> projectBaseLambdaQueryWrapper = lambdaQueryWrapper.eq(ProjectBase::getProjectName, projectBaseEntity.getProjectName())
                    .eq(ProjectBase::getDelFlag, DeleteStatus.OK.getCode());
            List<ProjectBase> projectBases = baseMapper.selectList(projectBaseLambdaQueryWrapper);
            if (!CollectionUtils.isEmpty(projectBases)){
                throw new ServiceException("项目名称已存在,请重新输入项目名称",500);
            }
            return true;
        }else {
            //校验数据是否存在
            if (ObjectUtil.isEmpty(projectBaseEntity.getProjectId())){
                throw new ServiceException("项目必要信息不存在,请联系管理员",500);
            }
            LambdaQueryWrapper<ProjectBase>lambdaQueryWrapper=new LambdaQueryWrapper<>();
            LambdaQueryWrapper<ProjectBase> projectBaseLambdaQueryWrapper = lambdaQueryWrapper.eq(ProjectBase::getProjectId, projectBaseEntity.getProjectId())
                    .eq(ProjectBase::getDelFlag, DeleteStatus.OK.getCode());
            ProjectBase projectBase = baseMapper.selectOne(projectBaseLambdaQueryWrapper);
            if (ObjectUtil.isNull(projectBase)){
                throw new ServiceException("项目信息不存在,请刷新页面后操作",500);
            }
            //校验名称是否重复
            lambdaQueryWrapper.eq(ProjectBase::getProjectName, projectBaseEntity.getProjectName())
                    .eq(ProjectBase::getDelFlag, DeleteStatus.OK.getCode())
                    .ne(ProjectBase::getProjectId,projectBaseEntity.getProjectId());
            List<ProjectBase> projectBases = baseMapper.selectList(projectBaseLambdaQueryWrapper);
            if (!CollectionUtils.isEmpty(projectBases)){
                throw new ServiceException("项目名称已存在,请重新输入项目名称",500);
            }
            return true;
        }
    }

    /**
     * 根据条件分页查询项目列表
     *
     * @param projectBaseEntity 项目管理信息
     * @return 项目管理集合信息
     */
    @Override
    public List<ProjectBaseEntity> selectProjectList(ProjectBaseEntity projectBaseEntity) {
        return baseMapper.selectProjectList(projectBaseEntity);
    }


}
